package com.jiawa.wiki.job;

import com.jiawa.wiki.mapper.DocMapperCustom;
import com.jiawa.wiki.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 电子书内容更新（点赞，阅读，文档数）
 */
@Component
public class DocJob {

    @Resource
    private DocMapperCustom docMapperCustom;

    @Resource
    private SnowFlake snowFlake;

    private static final Logger LOG = LoggerFactory.getLogger(DocJob.class);

    /**
     * 自定义cron表达式
     * 每60秒执行一次
     */
    @Scheduled(cron = "*/60 * * * * ?")
    public void updateEbookInfo() {
        // 增加日志流水号，也叫记录号
        MDC.put("LOG_ID", String.valueOf(snowFlake.nextId()));
        LOG.info("开始更新文档数据（60秒/次）");
        long start = System.currentTimeMillis();
        docMapperCustom.updateEbookInfo();
        LOG.info("更新文档数据完成，耗时：{}ms", System.currentTimeMillis() - start);
    }

}
